//Pietras93OS
//Shell
//pietras93@gmail.com
//

#include <system.h>

/*
void clrscr()
{
	cls();
	puts("Pietras93OS 0.0.1 \t\t\t\tWELCOME!\nSimple shell \t\t\t\tWELCOME!\n------------------------");
}
*/

void cons(char *path, char *user, char *compname)
{
	if(user == "")
	{
		user = "root";
	}
	if(path == "")
	{
		path = "/";
	}
	if(compname == "")
	{
		compname = "localhost";
	}
	settextcolor(2,0);
	puts("\n[");
	settextcolor(7,0);
	puts(user);
	settextcolor(4,0);
	puts("@");
	settextcolor(7,0);
	puts(compname);
	settextcolor(5,0);
	puts(" ");
	puts(path);
	settextcolor(2,0);
	puts("]");
	settextcolor(3,0);
	puts("$ ");
	settextcolor(7,0);
}

void taskmgr()
{
        int c, o;
		char g;
        printf("Process\t\tpid\towner\tpriority\n");
        for ( c=0; c<MAX_TASK; c++)
        {
                if(_tasks[c].status == TS_RUNNABLE)
                {
                        o = c;
                        // System processes start at 100
                        //if((!strcmp(_tasks[c].owner, "system"))) o = o + 100;

                        //printf("%s\t%i\t%s\t", _tasks[c].desc, o, _tasks[c].owner);
						puts(_tasks[c].desc);
						puts("\t");
						itoa(o, g);
						puts(g);
						puts("\t");
						puts(_tasks[c].owner);
						puts("\t");
                        char *pri = "";
                        if (_tasks[c].priority == 1) pri="Low";
                        if (_tasks[c].priority == 2) pri="Normal";
                        if (_tasks[c].priority == 3) pri="High";
                        if (_tasks[c].priority == 4) pri="Realtime";
                        puts(pri);
						puts("\n");
                }
                if(_tasks[c].status == TS_ZOMBIE)
                {
                        o = c;
                        // System processes start at 100
                        //if((!strcmp(_tasks[c].owner, "system"))) o = o + 100;

                        //printf("%s\t%i\t%s\t", _tasks[c].desc, o, _tasks[c].owner);
						puts(_tasks[c].desc);
						puts("\t");
						itoa(o, g);
						puts(g);
						puts("\t");
						puts(_tasks[c].owner);
						puts("\t");
                        char *pri = "";
                        if (_tasks[c].priority == 1) pri="Low";
                        if (_tasks[c].priority == 2) pri="Normal";
                        if (_tasks[c].priority == 3) pri="High";
                        if (_tasks[c].priority == 4) pri="Realtime";
						puts(pri);
                        puts("\tZOMBIE\n");
                }
        }
}
BOOL startswith(char* str1, char* str2)
{
    for (; *str1 == *str2; str1++, str2++);
    if (*str2 == 0)
        return 0;
    return 1;
}

void set_priority_high(int id)
{
	_tasks[id].priority = 3;
}

void parse(char* arg)
{
	int i;
	/*for(i = 0; i == j; i++)
	{
		if(strcmp(cmd[i], ' ') == 0)
		{
			k++;
			l = 0;
		} else
		{
			arg[k][l] = cmd[i];
			l++;
		}
	}*/
		if(strcmp(arg,"help") == 0)
		{
			puts("help - this command\ncls - clear screen\ntaskmgr - show task manager\ntask - show current task id\nkill <pid> - kill task with <pid>");
		} else 
		if(startswith(arg,"kill") == 0)
		{
			kill_task(2);
			puts(arg);
			char s;
			itoa(atoi(arg + 5), s);
			puts(s);
		} else
		if(startswith(arg,"setph") == 0)
		{
			//i = strlen(arg);
			int j;
			char s[5];
			do
			{
				if(j>=7)
					s[j] = arg[j];
				j++;				
			} while (*arg == "\0");
			set_priority_high(0);
			puts(arg);
			puts("\n");
			char ss;
			itoa(atoi(s), ss);
			puts(ss);
		} else
		if(strcmp(arg,"task") == 0)
		{
			i = gettask();
			char *strih;
			itoa(i, strih);
			puts("Task id: ");
			puts(strih);
		} else 
		if(strcmp(arg,"cls") == 0)
		{
			cls();
		} else 
		if(strcmp(arg,"taskmgr") == 0)
		{
			taskmgr();
		} else { puts("Unknown command. Type help for list.");
		puts(arg); //puts(arg[1]);
		}	
}

void shell()							//TODO:
{										//Rewrite with args; put in other file
	int i;
	char* t;

	//clrscr();
	cls();
	while(1)
	{
		cons("/", "root", "localhost");			//when fs will be working this will be improved
		t = gets();
		//char arg[5][100];
		parse(t);
		//result = *explode(t, delims);
		
		//char* token;
		//const char dlm[] = " ";
		//i = 0;
		//token = strtok(t,dlm);
   
		//while( token != NULL) {
        //   puts(token);
		//   i++;
        //   token = strtok(NULL," ");
		//}
		

		
		/*switch(arg[0])
		{
		case 'help' : puts("help - this command\ncls - clear screen\ntaskmgr - show task manager\ntask - show current task id\nkill <pid> - kill task with <pid>");
						break;

        case 'cls' : cls;
						break;

        case 'taskmgr' : taskmgr;
						break;

        case 'task' : show_task_id;
						break;	

        case 'kill' : kill_task(atoi(arg[1]));
						break;						
						
		default  : printf("Unknown command. Type help for list.");
					printf(arg[0]);
						break;
		}
		*/
	}
}

/*
int shell()
{
*/
